package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.HxDayReportMapper;
import com.ruoyi.system.mapper.HxProjectMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.domain.HxDayReport;
import com.ruoyi.system.domain.HxProject;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IHxProjectService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;

/**
 * 项目 服务层实现
 * 
 * @author ruoyi
 * @date 2019-07-31
 */
@Service
public class HxProjectServiceImpl implements IHxProjectService 
{
	@Autowired
	private HxProjectMapper hxProjectMapper;
	@Autowired
	private HxDayReportMapper hxDayReportMapper;
	@Autowired
	private SysUserMapper userMapper;

	/**
     * 查询项目信息
     * 
     * @param id 项目ID
     * @return 项目信息
     */
    @Override
	public HxProject selectHxProjectById(Integer id)
	{
    	HxProject project =  hxProjectMapper.selectHxProjectById(id);
    	String startDateStr = DateUtils.parseDateToStr("yyyy-MM-dd", project.getStartDate());
    	String endDateStr = DateUtils.parseDateToStr("yyyy-MM-dd", project.getEndDate());
    	project.setStartDateStr(startDateStr);
    	project.setEndDateStr(endDateStr);
    	String[] userIds = project.getParticipators().split(",");
    	String names = "";
    	for (int i = 0; i < userIds.length; i++) {
    		SysUser user = userMapper.selectUserById(Long.parseLong(userIds[i]));
    		names += user.getUserName()+",";
		}
    	project.setParticipatorsName(names);
		return project;
	}
	
	/**
     * 查询项目列表
     * 
     * @param hxProject 项目信息
     * @return 项目集合
     */
	@Override
	public List<HxProject> selectHxProjectList(HxProject hxProject)
	{
		List<HxProject> list = hxProjectMapper.selectHxProjectList(hxProject);
		for(int i=0;i<list.size();i++) {
			String [] userIds = list.get(i).getParticipators().split(",");
			String userName ="";
			for(int j=0 ;j<userIds.length;j++) {
				 userName += userMapper.selectUserById(Long.parseLong(userIds[j])).getUserName()+",";
			}
			list.get(i).setParticipatorsName(userName);
		}
	//	System.out.println(list);
	    return list;
	}
	
	@Override
	public List<HxProject> selectHxProjectList1(HxProject hxProject)
	{
		List<HxProject> list = hxProjectMapper.selectHxProjectList(hxProject);
	    return list;
	}
	
    /**
     * 新增项目
     * 
     * @param hxProject 项目信息
     * @return 结果
     */
	@Override
	public int insertHxProject(HxProject hxProject)
	{
	    return hxProjectMapper.insertHxProject(hxProject);
	}
	
	/**
     * 修改项目
     * 
     * @param hxProject 项目信息
     * @return 结果
     */
	@Transactional
	@Override
	public int updateHxProject(HxProject hxProject)
	{
		HxDayReport hxDayReport = new HxDayReport();
		hxDayReport.setProname(hxProject.getProname());
		hxDayReport.setProjectId(Long.parseLong(hxProject.getId().toString()));
		hxDayReportMapper.updateProjectName(hxDayReport);
	    return hxProjectMapper.updateHxProject(hxProject);
	}

	/**
     * 删除项目对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteHxProjectByIds(String ids)
	{
		return hxProjectMapper.deleteHxProjectByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<HxProject> selectProjectListByMonthAndType(String startDate,String endDate, Integer type) {
		return hxProjectMapper.selectProjectListByMonthAndType(startDate,endDate, type);
	}

	@Override
	public List<HxProject> selectParticipatedProjectList(String userId) {
		List<HxProject>  list =  hxProjectMapper.selectIsWorkingProject();
		for(int i=0;i<list.size();i++) {
			if(!list.get(i).getParticipators().contains(userId+",")) {
				list.remove(i);
			}
		}
		 return list;
	}
	
}
